﻿using PineLake.Drmwe.ConvertValue.ConvertEnumValue.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PineLake.Drmwe.ConvertValue.ConvertEnumValue
{
    /// <summary>
    /// 转换枚举类(键/值)
    /// </summary>
    public class ConvertsEnum : IConvertsEnum
    {

        /// <summary>
        /// （通过枚举类型获取所有枚举）(键)
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="value"></param>
        /// <returns></returns>
        public List<T> GetEnumKeyList<T>() where T : Enum
        {
            List<T> list = Enum.GetValues(typeof(T)).OfType<T>().ToList();
            return list;
        }
        /// <summary>
        /// （通过枚举类型获取所有枚举）键跟值
        /// </summary>
        /// <typeparam name="T">枚举类</typeparam>
        /// <returns></returns>
        public List<ConvertEnumToList> GetEnumList<T>() where T : Enum
        {
            List<ConvertEnumToList> EnumList = new List<ConvertEnumToList>();
            foreach (var Item in Enum.GetValues(typeof(T)))//枚举转List
            {
                ConvertEnumToList ConvertEnum = new ConvertEnumToList();
                object[] objArr = Item.GetType().GetField(Item.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), true);
                ConvertEnum.Key = Convert.ToInt32(Item);//key
                ConvertEnum.Value = Item.ToString();//value
                EnumList.Add(ConvertEnum);
            }
            return EnumList;
        }
        /// <summary>
        /// （通过枚举类型获取所有枚举）(值)
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public List<EnumValueList> GetEnumValueList<T>() where T : Enum
        {
            List<EnumValueList> EnumList = new List<EnumValueList>();
            foreach (var Item in Enum.GetValues(typeof(T)))//枚举转List
            {
                EnumValueList ConvertEnum = new EnumValueList();
                object[] objArr = Item.GetType().GetField(Item.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), true);
                ConvertEnum.Value = Item.ToString();//value
                EnumList.Add(ConvertEnum);
            }
            return EnumList;
        }
    }
}
